package com.example.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.eduservice.entity.EduCourse;
import com.example.eduservice.entity.EduCourseDescription;
import com.example.eduservice.entity.frontvo.CourseFrontVo;
import com.example.eduservice.entity.frontvo.CourseWebVo;
import com.example.eduservice.entity.vo.CourseInfoVo;
import com.example.eduservice.entity.vo.CoursePublishVo;
import com.example.eduservice.mapper.EduCourseMapper;
import com.example.eduservice.service.EduChapterService;
import com.example.eduservice.service.EduCourseDescriptionService;
import com.example.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.eduservice.service.EduVideoService;
import com.example.handler.GuliException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author xiuxian
 * @since 2022-06-26
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    //注入课程描述
    @Autowired
    private EduCourseDescriptionService courseDescriptionService;


    //注入小节
    @Autowired
    private EduVideoService videoService;

    //注入章节
    @Autowired
    private EduChapterService chapterService;



    //添加课程信息和简介
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {

        //想课程表中添加信息
        EduCourse eduCourse=new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        //想数据库加入课程信息
        int insert = baseMapper.insert(eduCourse);
        if(insert==0){
            //添加失败
            throw  new GuliException(20001,"添加课程失败");
        }

        String pid=eduCourse.getId();

        //向课程描述表中添加信息
        EduCourseDescription courseDescription=new EduCourseDescription();
        courseDescription.setDescription(courseInfoVo.getDescription());
        //设置和朱磊一样的id
        courseDescription.setId(pid);

        courseDescriptionService.save(courseDescription);


        return  pid;
    }

    //根据课程id查询信息
    @Override
    public CourseInfoVo getCourseInfo(String courseId) {

        EduCourse eduCourse=baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo=new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse,courseInfoVo);


        EduCourseDescription eduCourseDescription=courseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(eduCourseDescription.getDescription());



        return courseInfoVo;
    }

    //修改课程信息
    @Override
    public void updateCoureInfo(CourseInfoVo courseInfoVo) {

        //修改课程表
        EduCourse eduCourse=new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int index = baseMapper.updateById(eduCourse);
        if(index==0){
            throw  new GuliException(20001,"修改课程信息失败");
        }

        //修改描述表
        EduCourseDescription courseDescription=new EduCourseDescription();
        courseDescription.setId(courseInfoVo.getId());
        courseDescription.setDescription(courseInfoVo.getDescription());
        courseDescriptionService.updateById(courseDescription);


    }

    //根据课程id查询课程确认信息
    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);
        return publishCourseInfo;
    }

    //删除课程
    @Override
    public void removeCourse(String courseId) {
        //删除课程小节
        videoService.removeVideoByid(courseId);
        //删除课程章节
        chapterService.removeChapterByid(courseId);
        //删除课程描述
        courseDescriptionService.removeById(courseId);
        //删除课程
        int result = baseMapper.deleteById(courseId);
        if(result == 0) { //失败返回
            throw new GuliException(20001,"删除课程失败");
        }



    }

    //查询所有banner
    @Cacheable(value = "course", key = "'selectIndexcourse'")
    @Override
    public List<EduCourse> selectCourseList() {
        //查询前八名讲师
        QueryWrapper<EduCourse> coursewrapper=new QueryWrapper<>();
        coursewrapper.orderByDesc("id");
        //拼接sql语句
        coursewrapper.last("limit 8");
        List<EduCourse> courseList = baseMapper.selectList(coursewrapper);
        return courseList;
    }


    //条件查询带分页前端
    @Override
    public Map<String, Object> getFrontCourseList(Page<EduCourse> coursePage, CourseFrontVo courseFrontVo) {
        //构造条件
        QueryWrapper<EduCourse> wrapper=new QueryWrapper<>();
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())){  //判断一级标题是否为空
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectId())){
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());   //二级标题
        }

        if(!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())){
            //购买量，关注度           进行降序排列
            wrapper.orderByDesc("buy_count");
        }

        if(!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())){
            //最先创建时间     根据创建时间进行降序排列
            wrapper.orderByDesc("gmt_create");

        }
        if(!StringUtils.isEmpty(courseFrontVo.getPriceSort())){
            //价格排序      根据价格的高低序
            wrapper.orderByDesc("price");
        }


        baseMapper.selectPage(coursePage,wrapper);

        List<EduCourse> records = coursePage.getRecords();
        long current = coursePage.getCurrent();
        long total = coursePage.getTotal();
        long size = coursePage.getSize();
        long pages = coursePage.getPages();
        boolean hasPrevious = coursePage.hasPrevious();
        boolean hasNext = coursePage.hasNext();

        //把分页查询的结果放到map集合当中
        Map<String,Object> map=new HashMap<>();

        map.put("records",records);
        map.put("current",current);
        map.put("total",total);
        map.put("size",size);
        map.put("pages",pages);
        map.put("hasPrevious",hasPrevious);
        map.put("hasNext",hasNext);
        return map;

    }


    //跟局课程id查询课程信息
    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {


        return baseMapper.getBaseCourseInfo(courseId);
    }
}
